package com.lengmou.ctrl;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.lengmou.entity.Orders;
import com.lengmou.entity.OrdersInfo;
import com.lengmou.service.OrdersInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ordersinfo")
public class OrdersInfoController {
    @Resource
    private OrdersInfoService ordersInfoService;

    @RequestMapping(value = "/getAll",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getAll(int pageNum,int pageSize,Integer goodsid,Integer ordersid,Map<String,Object> map){
        JSONObject json = new JSONObject();
        map.put("goodsid",goodsid);
        map.put("ordersid",ordersid);
        PageHelper.startPage(pageNum,pageSize);
        List<OrdersInfo> search = ordersInfoService.search(map);
        json.put("count",ordersInfoService.search(map).size());
        json.put("data",search);
        json.put("code",0);
        return json.toString();
    }

    @RequestMapping(value = "/setNum",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setNum(OrdersInfo ordersInfo){
        JSONObject json = new JSONObject();
        if(ordersInfo!=null){
            int i = ordersInfoService.setInfoNum(ordersInfo);
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value = "/setGoodsid",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setGoodsid(OrdersInfo ordersInfo){
        JSONObject json = new JSONObject();
        if(ordersInfo!=null){
            int i = ordersInfoService.setInfoGoodsId(ordersInfo);
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value = "/setOrdersid",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setOrdersid(OrdersInfo ordersInfo){
        JSONObject json = new JSONObject();
        if(ordersInfo!=null){
            int i = ordersInfoService.setInfoOrdersId(ordersInfo);
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }

//    添加
    @RequestMapping(value = "/addInfo",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String addInfo(OrdersInfo info){
        JSONObject json = new JSONObject();
        if(info!=null){
            int i = ordersInfoService.addOrdersInfo(info);
            json.put("state",i);
        }else{
//            传过来的订单详情是空的
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value = "/deleteAll",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String deleteAll( @RequestBody List<OrdersInfo> infos){
        JSONObject json = new JSONObject();
        if(infos!=null&&infos.size()>0){
            int i = 0;
            for (OrdersInfo o:infos) {
                i+=ordersInfoService.deleteOrdersInfo(o.getId());
            }
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }





}
